package com.qinghe.pro.controller.main.api;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qinghe.pro.entity.api.ApiColumn;
import com.qinghe.pro.service.api.ApiColumnService;
import com.qinghe.utils.AjaxObject;
import com.qinghe.utils.Page;

@Controller
@RequestMapping("/manager/api/column")
public class ApiColumnController {

	@Autowired
	@Qualifier("apiColumnServiceImpl")
	private ApiColumnService apiColumnService;

	private static final String CREATE = "manager/api/column/create";
	private static final String UPDATE = "manager/api/column/update";
	private static final String VIEW = "manager/api/column/view";
	private static final String LIST = "manager/api/column/list";

	@RequiresPermissions("ApiColumn:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate(Map<String, Object> map) {
        map.put("columnList", apiColumnService.findAllAsTree());
		return CREATE;
	}

	@RequiresPermissions("ApiColumn:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody AjaxObject create(ApiColumn apiColumn) {
		apiColumnService.insert(apiColumn);

		return AjaxObject.newOk("添加接口分类成功！");
	}

	@RequiresPermissions("ApiColumn:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable Integer id, Map<String, Object> map) {
		ApiColumn column = apiColumnService.get(id);

		map.put("column", column);
        map.put("columnList", apiColumnService.findAllAsTree());
		return UPDATE;
	}

	@RequiresPermissions("ApiColumn:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody AjaxObject update(ApiColumn apiColumn) {
		apiColumnService.update(apiColumn);

		return AjaxObject.newOk("修改接口分类成功！");
	}

	@RequiresPermissions("ApiColumn:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody AjaxObject delete(@PathVariable Integer id) {
		apiColumnService.delete(id);

		return AjaxObject.newOk("删除接口分类成功！");
	}

	@RequiresPermissions("ApiColumn:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody AjaxObject deleteMany(@RequestParam("ids[]") List<Integer> ids) {
		for (Integer id : ids) {
			apiColumnService.delete(id);
		}

		return AjaxObject.newOk("删除接口分类成功！");
	}

	@RequiresPermissions("ApiColumn:view")
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable Integer id, Map<String, Object> map) {
		ApiColumn apiColumn = apiColumnService.get(id);
		
		map.put("apiColumn", apiColumn);
		return VIEW;
	}

	@RequiresPermissions("ApiColumn:show")
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Page page, Map<String, Object> map) {
        map.put("columnList", apiColumnService.findAllAsTree());

		return LIST;
	}

}
